<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../ui/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../ui/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="candyForm">
    <input name="id" id="id" style="display: none;"/>
    <input type="text" name="catalogName" id="catalogName"  class="layui-hide" value=""/>
    <input name="tags" id="tags" style="display: none;"/>
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" id="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">所属分类</label>
        <div class="layui-input-block">
            <input type="text" name="catalogId" id="catalogId" lay-filter="catalogId" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">摘要</label>
        <div class="layui-input-block">
            <textarea name="description" id="description" placeholder="请输入摘要" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">标签</label>
        <div class="layui-input-block">
            <div id="tagsDiv" style="width: 300px; margin: 20px;"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">发布状态</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-skin="switch" lay-text="ON|OFF" name="status" id="status">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">发布时间</label>
        <div class="layui-input-block">
            <input type="text" name="pulishTime" id="pulishTime" readonly="true" name="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">序号</label>
        <div class="layui-input-block">
            <input type="text" name="orderNum" id="orderNum" placeholder="请输入序号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">置顶</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-skin="switch" lay-text="ON|OFF" name="isTop" id="isTop">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">热点</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-skin="switch" lay-text="ON|OFF" name="isHot" id="isHot">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../ui/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../ui/layuimini/js/lay-config.js"></script>
<script>
    layui.use(['form','auth', 'laydate','treeSelect','xmSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            treeSelect = layui.treeSelect,
            xmSelect = layui.xmSelect,
            laydate = layui.laydate,
            $ = layui.$;
        //日期
        laydate.render({
            elem: '#pulishTime'
        });

        var id = getQueryString("id");
        var submitType="POST";
        var api="/api/cms/article";
        if(typeof(id)!="undefined" && id!=null && id.length>=0)
        {
            var settings = {
                "url": api+"/"+id,
                "method": "GET"
            };
            $.ajax(settings).done(function (res) {
                form.val("candyForm", res.data);
                loadOther(true);
            });
            submitType="PUT"
        }
        else
        {
            loadOther(false);
        }
        function loadOther(isInit)
        {
            treeSelect.render({
                // 选择器
                elem: '#catalogId',
                // 数据
                data: "/api/cms/catalog/tree",
                // 异步加载方式：get/post，默认get
                type: 'get',
                // 占位符
                placeholder: '请选择机构',
                // 是否开启搜索功能：true/false，默认false
                search: true,
                // 点击回调
                click: function(data){
                    form.val("candyForm", {"catalogName":data.current.name,"catalogId":data.current.id});
                },
                // 加载完成后的回调函数
                success: function (d) {
                    if(isInit) {
                        treeSelect.checkNode("catalogId", form.val("candyForm")["catalogId"]);
                    }
                }
            });

            var settings = {
                "url": "/api/cms/tag/list",
                "method": "GET"
            };
            $.ajax(settings).done(function (res) {
                //渲染多选
                var tags = xmSelect.render({
                    prop: {
                        name: 'name',
                        value: 'id',
                    },
                    name: "tagsDiv",
                    el: '#tagsDiv',
                    data: res.data.records
                });
                var checkedTags = form.val("candyForm")["tags"];
                if(checkedTags!=null && checkedTags.length>0)
                {
                    tags.setValue(checkedTags.split(","));
                }
            });
        }
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field["status"] = data.field["status"] ? 1 : 0
            data.field["isTop"] = data.field["isTop"] ? 1 : 0
            data.field["isHot"] = data.field["isHot"] ? 1 : 0
            data.field["tags"] = data.field["tagsDiv"]
			var settings = {
			  "url": api,
			  "method": submitType,
              "contentType":"application/json",
			  "data": JSON.stringify(data.field),
			};

			$.ajax(settings).done(function (response) {
			    if(response.code==200)
                {
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                    parent.reload();
                }
			    else
                {
                    layer.alert(response.msg,{
                        icon: 2
                    });
                }
			}); 
            return false;
        });
    });
</script>
</body>
</html>